package com.xuhaijian.utils;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateUtils {
    private static final String ONE_DAY_START =" 00:00:00";
    private static final String ONE_DAY_END =" 23:59:59";
    private static final String FULL_DATE_FORMAT ="yyyy-MM-dd HH:mm:ss";
    /**
     * 转换成字符串时间格式
     * @param l 时间
     * @param patternString 格式
     * @return 时间字符串
     */
    public static String LocalDateString(LocalDate l, String patternString){
        if (l == null){
            return null;
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(patternString);
        return dtf.format(l);
    }

    /***
     * 转化成LocalDate
     * @param dateString 字符串String
     * @return
     */
    public static LocalDate StringLocalDate(String dateString){
        if (dateString == null){
            return null;
        }
        return LocalDate.parse(dateString);
    }
    /***
     * 转化成LocalDateTime
     * @param dateString 字符串String
     * @return
     */
    public static LocalDateTime StringLocalDateTime(String dateString,String format){
        if (dateString == null){
            return null;
        }
        if(format == null){
            format = FULL_DATE_FORMAT;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(dateString,formatter); // 转换为 LocalDate
    }

    /***
     *
     * @param localDate 当前日期
     * @param num 计算日期
     * @return
     */
    public static LocalDate computeNow(LocalDate localDate,Integer num){
        if (localDate == null){
            return null;
        }
        return localDate.minusDays(num);
    }

    public static String getOneDayStart(String oneDayStart){
        return oneDayStart+ONE_DAY_START;
    }

    public static String getOneDayEnd(String oneDayEnd){
        return oneDayEnd+ONE_DAY_END;
    }




    public static void main(String[] args) {
//        System.out.println(LocalDateString(LocalDate.now(), "yyyy-MM-dd"));
//        System.out.println(getOneDayStart("2024-01-01"));
//        System.out.println(getOneDayEnd("2024-01-01"));
//        System.out.println(StringLocalDate("2024-09-01"));
//        System.out.println(StringLocalDateTime("2024-09-01 00:00:00",null));
//        System.out.println(computeNow(LocalDate.now(),-1));
//
        System.out.println(LocalDate.now());
        LocalDate localDate = LocalDate.now().minusDays(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        LocalDateTime endOfDay = localDate.atTime(23, 59, 59);

        System.out.println(startOfDay);
        System.out.println(endOfDay);

    }
}
